/*
 * File:   AP.cpp
 * Author: liuyi
 *
 * Created on August 31, 2009, 3:45 PM
 */

#include "Toolkit.h"

#include <cstdlib>
#include <cassert>
#include <string>
#include <sstream>

using namespace std;

IndexOutOfBoundsException::IndexOutOfBoundsException(size_t i) {
    ostringstream s;
    s << "Invalid index " << i;
    m_error = s.str();
}

string IndexOutOfBoundsException::what() {
    return "IndexOutOfBoundsException: " + m_error;
}

IllegalArgumentException::IllegalArgumentException(string s) : m_error(s) {
}

string IllegalArgumentException::what() {
    return "IllegalArgumentException: " + m_error;
}

FileNotFoundException::FileNotFoundException(string s) : m_error(s) {
}

string FileNotFoundException::what() {
    return "IllegalStateException: " + m_error;
}

NotSupportedException::NotSupportedException(string s) : m_error(s) {
}

string NotSupportedException::what() {
    return "NotSupportedException: " + m_error;
}

int64_t Toolkit::hashIntArray(const int *data, int size) {
    int64_t code = 0;
    for (int i = 0; i < size; i++) {
        code = ((code << 2) + (data[i] >> 4)) ^ (data[i] << 10);
    }

    return code;
}

void Random::generate(double *list, int len) {
	for(int i = 0; i < len; ++i) {
		list[i] = generate();
	}
}

void Random::genRandomArray(float *list, int len) {
	for(int i = 0; i < len; ++i) {
		list[i] = (float) generate();
	}
}

double UniformRandom::generate() {
	double diff = m_max - m_min;
	return m_min + diff*(rand() + m_shift1)/((double)RAND_MAX + m_shift1 + m_shift2);
}

double GuassRandom::generate() {
	double u1 = m_uniformRandom.generate();
	double u2 = m_uniformRandom.generate();
	double temp = sqrt(-2 * log(u1)) * sin(2 * Constant::PI * u2);

	return m_variance*temp + m_mean;
}
